import java.util.Random;
import java.util.Scanner;

/**
 * @author 10277
 */
public class GuessNumber {
    public static void main(String[] args) {
            game();
    }

    /**
     *随机生成一个数字并且传到exp方法内
     */
    public static void game() {
        Random random = new Random();
        int guess = random.nextInt(100) + 1;
        exp(guess);
    }

    /**
     * 猜数字完成后，接受玩家输入的值进行二次游玩判断
     */
    public static void input(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("继续游玩请输入y，退出游戏请输入n");
        String input=scanner.next();
        if ("y".equals(input)){
            game();
        }else if ("n".equals(input)){
            System.out.println("感谢您的游玩，再见");
        }
    }

    /**
     * 接受生成的随机数，并进行判断
     */
    public static void exp(int guess) {
        Scanner scanner = new Scanner(System.in);
            int i = 7;
            int number;
            for (int j = 0; j < i; j++) {
                System.out.println("请输入你猜到的数字");
                try {
                    number = scanner.nextInt();
                }catch (Exception e){
                    System.out.println("请输入数字");
                    break;
                }
                if (number<101&&number>0) {
                    if (guess < number) {
                        System.out.println("ao！猜的数字大了哦");
                    } else if (guess > number) {
                        System.out.println("ao！猜的数字有些小哦");
                    } else {
                        System.out.println("恭喜你！猜对了," + "你一共猜了" + (j + 1) + "次");
                        break;
                    }
                    if (j != 6) {
                        System.out.println("你还有" + (i - j - 1) + "次机会");
                    } else {
                        System.out.println("你的机会用完了"+"正确答案是"+guess);
                    }
                }else {
                    System.out.println("你输入的数字不在猜测范围之内");
                    break;
                }
            }
            input();
    }
}

